package com.assignwork.service.impl;

import com.assignwork.dto.ShiftRuleDailyShiftSubmitDTO;
import com.assignwork.dto.ShiftRuleTimeSpaceSubmitDTO;
import com.assignwork.entity.ShiftRuleDailyShift;
import com.assignwork.entity.ShiftRuleTimeSpace;
import com.assignwork.mapper.ShiftRuleDailyShiftMapper;
import com.assignwork.service.IShiftRuleDailyShiftService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ShiftRuleDailyShiftServiceImpl
 * @Description 班组生产规则接口实现
 * @Author dujianyu
 * @Date 2023/12/1 15:58
 */
@Service
public class ShiftRuleDailyShiftServiceImpl extends BaseServiceImpl<ShiftRuleDailyShiftMapper, ShiftRuleDailyShift> implements IShiftRuleDailyShiftService {

    @Override
    public boolean removeSRDailyShiftsByRuId(Long ruleId) {
        QueryWrapper<ShiftRuleDailyShift> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(ShiftRuleDailyShift::getId)
                .eq(ShiftRuleDailyShift::getIsDeleted , 0)
                .eq(ShiftRuleDailyShift::getRuleId, ruleId);
        List<ShiftRuleDailyShift> sRDailyShiftsByRuleId = baseMapper.selectList(queryWrapper);
        if (Func.isEmpty(sRDailyShiftsByRuleId)) {
            return true;
        } else {
            List<Long> ids = sRDailyShiftsByRuleId.stream().map(ShiftRuleDailyShift::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
    }

    @Override
    public boolean submitSRDailyShiftBatch(List<ShiftRuleDailyShiftSubmitDTO> dailyShifts) {
        List<ShiftRuleDailyShift> entityList = new ArrayList<>();
        for (ShiftRuleDailyShiftSubmitDTO dailyShift : dailyShifts) {
            ShiftRuleDailyShift entity = new ShiftRuleDailyShift();
            entity.setId(dailyShift.getId());
            entity.setRuleId(dailyShift.getRuleId());
            entity.setCode(dailyShift.getCode());
            entity.setDailyShift(dailyShift.getDailyShift());
            entityList.add(entity);
        }

        if (Func.isEmpty(entityList)) {
            return true;
        } else {
            return super.saveOrUpdateBatch(entityList);
        }
    }

    @Override
    public Map<Integer, String> selectSRDailyShiftMapByRuleId(Long ruleId) {
        QueryWrapper<ShiftRuleDailyShift> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(ShiftRuleDailyShift::getCode,ShiftRuleDailyShift::getDailyShift)
                .eq(ShiftRuleDailyShift::getIsDeleted , 0)
                .eq(ShiftRuleDailyShift::getRuleId, ruleId)
                .orderByAsc(ShiftRuleDailyShift::getCode);
        List<ShiftRuleDailyShift> sRDailyShiftsByRuleId = baseMapper.selectList(queryWrapper);
        return sRDailyShiftsByRuleId.stream().collect(Collectors.toMap(ShiftRuleDailyShift::getCode, ShiftRuleDailyShift::getDailyShift, (key1 , key2) -> key1));
    }
}
